package com.gl.goods.base;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;

import androidx.multidex.MultiDex;

import com.gl.goods.R;
import com.gl.goods.httpclint.MyOkHttp;
import com.gl.goods.widght.CrashHandler;
import com.gl.library.tool.RxTool;
import com.gl.library.utils.SharedPreferencesUtils;
import com.hjq.toast.ToastUtils;
import com.simple.spiderman.SpiderMan;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class MyApplication extends Application {
    private static MyApplication context;
    private static MyApplication instance;
    private String login_token;
    private MyOkHttp mMyOkHttp;

    public static MyApplication getAppContext() {
        return context;
    }

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        instance = this;
        RxTool.init(this);
        SpiderMan.setTheme(R.style.SpiderManTheme_Dark);
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
        ToastUtils.init(this);
        MultiDex.install(this);
        //自定义OkHttp
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .build();
        mMyOkHttp = new MyOkHttp(okHttpClient);
        /**
         * 设置时区为北京时区
         */
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        TimeZone.setDefault(timeZone);
//        CrashReport.initCrashReport(getApplicationContext(), "7312fd14d7", false);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

    //单例
    public static synchronized MyApplication getInstance() {
        return instance;
    }
    public MyOkHttp getMyOkHttp() {
        return mMyOkHttp;
    }

    public String getLogin_token() {
        return login_token;
    }

    public void setLogin_token(String login_token) {
        this.login_token = login_token;
    }

    //判断是否登录
    public boolean isLogin() {
        //添加字段判断是否登录，用于完全退出
        boolean isLogin = (boolean) SharedPreferencesUtils.getParam(getApplicationContext(), SharedPreferencesUtils.isLogin, false);

        return isLogin;
    }

    //退出登录
    public void logout() {
        SharedPreferencesUtils.setParam(getApplicationContext(), SharedPreferencesUtils.isLogin, false);
    }

    /**
     * 重写 getResource 方法，防止系统字体影响
     */
    @Override
    public Resources getResources() {//禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            android.content.res.Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }
}
